package com.siyuanren.gcocp.common.interceptor;

import com.siyuanren.gcocp.common.exception.BaseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.ThrowsAdvice;

import java.lang.reflect.Method;

/**
 * Created by   guwr
 * Project_name education-api-parent
 * Path         com.siyuanren.gcocp.common.interceptor.ExceptionInterceptorLog
 * Date         2017/5/8
 * Time         13:03
 * Description
 */
public class ExceptionInterceptorLog implements ThrowsAdvice {

    private static Logger logger = LoggerFactory.getLogger(ExceptionInterceptorLog.class);

    /**
     * 对未知异常的处理. <br>
     * Method method 执行的方法 Object[] args <br>
     * 方法参数 Object target <br>
     * 代理的目标对象 Throwable BaseException 产生的异常 <br>
     */
    public void afterThrowing(Method method, Object[] args, Object target, BaseException ex) {
        logger.info("==>ExceptionInterceptorLog.BaseException");
        logger.info("==>errCode:" + ex.getCode() + " errMsg:" + ex.getMsg());
        logger.info("==>" + ex.fillInStackTrace());
    }

    public void afterThrowing(Method method, Object[] args, Object target, Exception ex) {

        logger.error("==>ExceptionInterceptorLog.BaseException");

        logger.error("==>Error class: " + target.getClass().getName());
        logger.error("==>Error method: " + method.getName());

        for (int i = 0; i < args.length; i++) {
            logger.error("==>args[" + i + "]: " + args[i]);
        }

        logger.error("==>Exception class: " + ex.getClass().getName());
        logger.error("==>" + ex.fillInStackTrace());
    }
}
